package com.itheima.web.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.itheima.pojo.Option;
import com.itheima.pojo.Question;
import com.itheima.service.QuestionService;
import com.itheima.service.impl.QuestionServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;

@WebServlet("/question/*")
public class QuestionServlet extends BaseServlet{
    private QuestionService questionService = new QuestionServiceImpl();

    public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1. 接收品牌数据
        BufferedReader br = request.getReader();
        String params = br.readLine();//json字符串


        //转为Brand对象
        Question question = JSON.parseObject(params, Question.class);

        //2. 调用service添加
        questionService.add(question);

        //3. 响应成功的标识
        response.getWriter().write("success");
    }

    public void selectAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        BufferedReader br = request.getReader();
        String params = br.readLine();//json字符串

        int sid = 1;

        if(params !=null && !params .equals("")){
            //4. 调用方法
           sid = Integer.parseInt(params);

        }

        List<Question> questions = questionService.selectAll(sid);
        //2. 转为JSON
        String jsonString = JSON.toJSONString(questions);
        //3. 写数据
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }

    public void addOption(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1. 接收选项数据
        BufferedReader br = request.getReader();
        String params = br.readLine();//json字符串
        Option option = new Option();
        option.setOptionName(params);
        String _questionId = request.getParameter("questionId");
        String _surveyId = request.getParameter("surveyId");

        int questionId = Integer.parseInt(_questionId);
        int surveyId = Integer.parseInt(_surveyId);
        option.setQuestionId(questionId);
        option.setSurveyId(surveyId);

        questionService.addOption(option);

        //3. 响应成功的标识
        response.getWriter().write("success");

    }

    public void selectOption(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String _questionId = request.getParameter("questionId");
        int questionId = Integer.parseInt(_questionId);

        List<Option> options = questionService.selectOption(questionId);

        //2. 转为JSON
        String jsonString = JSON.toJSONString(options);
        //3. 写数据
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }
}
